package com.one.common.view.dialog;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.one.common.R;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.dialog.binder.DialogListBinder;
import com.one.common.view.dialog.item.DialogListItem;
import com.one.common.view.multitytype.MultiTypeAdapter;

import java.util.ArrayList;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2018/11/16 10:34
 * Description：底部弹窗
 **/
public class BottomListDialog extends BaseDialog implements DialogListBinder.ItemClickListener {

    private ArrayList<DialogListItem> items;
    private DialogListBinder.ItemClickListener itemClickListener;

    public BottomListDialog(Context context, ArrayList<DialogListItem> items) {
        super(context, R.layout.dialog_bottom_list);
        this.items = items;
    }

    @Override
    public void initView() {
        super.initView();
        setShowBottom();
        RecyclerView rvList = view.findViewById(R.id.rc_list);
        MultiTypeAdapter adapter = new MultiTypeAdapter();
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter.register(DialogListItem.class, new DialogListBinder(this));
        rvList.setAdapter(adapter);
        if(ListUtils.isNotEmpty(items)){
            adapter.setItems(items);

        }
        view.findViewById(R.id.tv_btn_cancel).setOnClickListener(v -> dismiss());
    }


    @Override
    public void onClick(DialogListItem item, int position) {
        if (itemClickListener != null) {
            itemClickListener.onClick(item, position);
        }
        dismiss();
    }

    public void setItemClickListener(DialogListBinder.ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
